import React from 'react';
import app from '../../app.json';
import Ugame from '../../Ugame';
import REQUEST from "../../REQUEST";
export default class Httpc extends React.Component {
    static postFrom(url, data, callback) {
        var fetchOptions = {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: data
        };
        fetch(this.GetAPiValue(url), fetchOptions).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));
        }).done();
    }

    /**
     *url :请求地址
     *data:参数(Json对象)
     *callback:回调函数
     */
    static postJson (url,data,callback){
        var fetchOptions = {
            method:'POST',
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        };
        fetch(this.GetAPiValue(url), fetchOptions).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));
        }).done();
    }
    /**
     *url :请求地址
     *data:参数(Json对象)
     *callback:回调函数
     */
    static postQueryJson (url,data,callback){
        var fetchOptions = {
            method:'POST',
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        };
        let http_url = this.GetAPiValue(url)+"?token="+Ugame.info.Token;
        //console.log(http_url);
        fetch(http_url, fetchOptions).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));
        }).done();
    }

    /**
     *url :请求地址
     *data:参数(Json对象)
     *callback:回调函数
     */
    static postQueryJsonFromData (url,params,callback){
        var fetchOptions = {
            method:'POST',
            headers:{
                'Accept': 'application/json',   
                'Content-Type': 'text/json'
            },
        };    
        let http_url = this.GetAPiValue(url)+this.toQueryString(params);
        fetch(http_url, fetchOptions).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));
        }).done();
    }

    /**
     *url :请求地址
     *data:参数(Json对象)
     *callback:回调函数
     */
    static postFormData(url,data,callback){
        let formData = new FormData();
        formData.append('head', data.head);
        var fetchOptions = {
            method:'POST',
            body:formData
        };
        let http_url = this.GetAPiValue(url)+"?token="+Ugame.info.Token;
        fetch(http_url,fetchOptions).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));
        }).done();
    }

    //get请求
    /**
     *url :请求地址
     *callback:回调函数
     */
    static get(url,params,callback) {
        let http_url = this.GetAPiValue(url)+this.toQueryString(params);
        fetch(http_url).then((response) => response.text()).then((responseText) => {
            callback(JSON.parse(responseText));            
        }).done();
    }
    static getToHtml(url,params,callback) {
        let http_url = this.GetAPiValue(url)+this.toQueryString(params);
        //console.log(http_url);
        fetch(http_url).then((response) => response.text()).then((responseText) => {
            callback(responseText);
        }).done();
    }
    static GetAPiValue(api){
        return app.base_url+":"+app.port+api;
    }
    static toQueryString(obj){
        var qstring = "?";
        var array=[];
        for (var k in obj) {
            array.push(k+'='+obj[k]);
        }
        qstring +=array.join('&');
        return qstring;
    }
    static toQueryString2(obj){
        var qstring = "&";
        var array=[];
        for (var k in obj) {
            array.push(k+'='+obj[k]);
        }
        qstring +=array.join('&');
        return qstring;
    }
    static imgPate(path){
        if (path){
            if (path.startsWith('http://') || path.startsWith('https://')) {
                return path;
            }
        }
        return app.base_url+":"+app.port+path;
    }
}



